package org.swim.sessions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;
//import org.swim.entity.FakeNotificabile;
import org.swim.entity.*;
import org.swim.entity.Abilita.Stato;

/**
 * Session Bean implementation class GestoreAbilita
 */
@Stateless
public class GestoreAbilita implements GestoreAbilitaRemote{
	
	/**
     * Default constructor. 
     */
    public GestoreAbilita() {
        // TODO Auto-generated constructor stub
    }
    
    @PersistenceContext(unitName = "JPADB")
    private EntityManager am;

	@Override
	public Abilita addAbilita(String nome) {
		Abilita ab = new Abilita();
		ab.setNome(nome.toLowerCase());
		am.persist(ab);
	
		Integer cont = am.createNamedQuery("PropostaAbilita.getPropostaByName&Status")
				.setParameter("nome", nome)
				.setParameter("status", Status.INATTESA)
				.getResultList().size();
		
		if(cont > 0){
			this.accettaProposta(nome);
		}
		return ab;
	}

	@Override
	public void removeAbilita(String nome) {
		
		String query="DELETE FROM feedback "
				+" WHERE id = "
				+" (SELECT r.id FROM richiesta_aiuto r WHERE r.abilita = '"+ nome +"')";
		am.createNativeQuery(query, Feedback.class).executeUpdate();
		
		query="DELETE FROM richiesta_aiuto "
				+" WHERE abilita = '"+ nome + "'";
		am.createNativeQuery(query, RichiestaAiuto.class).executeUpdate();
		
		query="DELETE FROM utente_abilita "
				+" WHERE abilita = '"+ nome + "'";
		am.createNativeQuery(query, Abilita.class).executeUpdate();
		
		query="DELETE FROM utente_proposta "
				+" WHERE nomeAbilita = '"+ nome + "'";
		am.createNativeQuery(query, Utente.class).executeUpdate();
		
		query="DELETE FROM proposta_abilita "
				+" WHERE nomeAbilita = '"+ nome + "'";
		am.createNativeQuery(query, PropostaAbilita.class).executeUpdate();
		
		query="DELETE FROM abilita "
				+" WHERE nome = '"+ nome +"'";
		am.createNativeQuery(query, Abilita.class).executeUpdate();
		
		try{
			
			this.rifiutaProposta(nome);
			
		}catch(Exception e){
			
		}
		
	}

	@Override
	public List<Abilita> getAbilitaVaide() {
		return am.createNamedQuery("Abilita.getAbilitaValide").getResultList();
	}

	@Override
	public PropostaAbilita addProposta(String idUtente, String nome) {
		
		nome= nome.toLowerCase();
		
		Utente u = (Utente)am.createNamedQuery("Utente.getUtenteByEmail")
				.setParameter("email", idUtente)			
				.getSingleResult();
	
		Integer cont = am.createNamedQuery("Abilita.getAbilitaByName")
				.setParameter("nome", nome)
				.getResultList().size();

		if(cont > 0){
			return null;
		}
		
		PropostaAbilita pa;
		try {
			pa = (PropostaAbilita)am.createNamedQuery("PropostaAbilita.getPropostaByName")
									.setParameter("nome", nome)
									.getSingleResult();
		} catch (Exception e) {
			pa = new PropostaAbilita();
			pa.setNomeAbilita(nome.toLowerCase());
			pa.setStatus(Status.INATTESA);
			am.persist(pa);
		}
		
		if(! u.getPropostePresentate().contains(pa)){
			pa.setDataModifica(new Date(System.currentTimeMillis()));
			u.addPropostePresentate(pa);
			am.merge(u);
			return pa;
		}
		return pa;
	}

	@Override
	public PropostaAbilita accettaProposta(String nome) {
		PropostaAbilita pa = (PropostaAbilita)am.createNamedQuery("PropostaAbilita.getPropostaByName")
				.setParameter("nome", nome)
				.getSingleResult();
		pa.setDataModifica(new Date(System.currentTimeMillis()));
		pa.setStatus(Status.ACCETTATA);
		am.merge(pa);
		Abilita ab = new Abilita();
		ab.setNome(nome.toLowerCase());
		am.persist(ab);
		return pa;
	}

	@Override
	public PropostaAbilita rifiutaProposta(String nome) {
		PropostaAbilita pa = (PropostaAbilita)am.createNamedQuery("PropostaAbilita.getPropostaByName")
				.setParameter("nome", nome)
				.getSingleResult();
		pa.setDataModifica(new Date(System.currentTimeMillis()));
		pa.setStatus(Status.RIFIUTATA);
		am.merge(pa);
		return pa;
	}
    
    
}
